package com.punai.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.DateUtils;
import com.punai.common.utils.StringUtils;
import com.punai.standard.domain.StaVersion;
import com.punai.standard.domain.constants.StaConstants;
import com.punai.standard.mapper.StaVersionMapper;
import com.punai.standard.service.IStaVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 版本Service业务层处理
 *
 * @author zxw
 * @date 2022-12-13
 */
@Service
public class StaVersionServiceImpl extends ServiceImpl<StaVersionMapper,StaVersion> implements IStaVersionService
{
    @Autowired
    private StaVersionMapper staVersionMapper;

    /**
     * 查询版本
     *
     * @param id 版本主键
     * @return 版本
     */
    @Override
    public StaVersion selectStaVersionById(String id)
    {
        return staVersionMapper.selectStaVersionById(id);
    }

    /**
     * 查询版本列表
     *
     * @param staVersion 版本
     * @return 版本
     */
    @Override
    public List<StaVersion> selectStaVersionList(StaVersion staVersion)
    {
        return staVersionMapper.selectStaVersionList(staVersion);
    }

    /**
     * 新增版本
     *
     * @param staVersion 版本
     * @return 结果
     */
    @Override
    public int insertStaVersion(StaVersion staVersion)
    {
            return staVersionMapper.insert(staVersion);
    }

    /**
     * 修改版本
     *
     * @param staVersion 版本
     * @return 结果
     */
    @Override
    public int updateStaVersion(StaVersion staVersion)
    {
        return staVersionMapper.updateById(staVersion);
    }

    /**
     * 批量删除版本
     *
     * @param ids 需要删除的版本主键
     * @return 结果
     */
    @Override
    public int deleteStaVersionByIds(String[] ids)
    {
        return staVersionMapper.deleteStaVersionByIds(ids);
    }

    /**
     * 删除版本信息
     *
     * @param id 版本主键
     * @return 结果
     */
    @Override
    public int deleteStaVersionById(String id)
    {
        return staVersionMapper.deleteStaVersionById(id);
    }

    @Override
    public List<StaVersion> getVersionList(String type,Long companyId) {
        return staVersionMapper.selectListBySpecId(companyId,type,null);
//        return staVersionMapper.selectList(
//                Wrappers.<StaVersion>lambdaQuery()
//                        .eq(StaVersion::getType,type)
//                        .eq(StaVersion::getCompanyId, companyId)
//                .orderBy(true,false,StaVersion::getCreateTime)
//        );
    }

    @Override
    public String getNewestId(Long companyId,String type,String specId) {
        return staVersionMapper.getNewestId(companyId,type,specId);
    }

    @Override
    public StaVersion getNewestVersionsId(String type, Long companyId) {
        return staVersionMapper.selectOne(new LambdaQueryWrapper<StaVersion>().eq(StaVersion::getType,type).eq(StaVersion::getCompanyId,companyId).orderByDesc(StaVersion::getCreateTime).last("limit 1"));
    }

    @Override
    public int coverPublish(StaVersion staVersion,Long companyId) {
        String type = staVersion.getType();
        String versionsId = staVersion.getId();
        if(StringUtils.equals(type, StaConstants.CL_TYPE)){
            staVersionMapper.coverPublishCL(companyId,versionsId);
        }else if(StringUtils.equals(type, StaConstants.JX_TYPE)){
            staVersionMapper.coverPublishJX(companyId,versionsId);
        }else {
            throw new ServiceException("类型错误");
        }
        return 200;
    }

    @Override
    @Transactional
    public int newPublish(StaVersion staVersion,Long companyId) {
        String type = staVersion.getType();
        String name = DateUtils.dateTimeNow("yyyyMMddHHmmssSSS");
        Integer maxVerNum = staVersionMapper.getMaxVerNum(type);
        StaVersion staVersionNew = new StaVersion();
        staVersionNew.setCompanyId(companyId);
        staVersionNew.setType(type);
        staVersionNew.setName(name);
        staVersionNew.setVerNum(++maxVerNum);
        staVersionMapper.insert(staVersionNew);
        // 创建新版本成功
        if(StringUtils.equals(type,StaConstants.CL_TYPE)){
            staVersionMapper.publishCL(companyId,staVersionNew.getId());
        } else if (StringUtils.equals(type, StaConstants.JX_TYPE)) {
            staVersionMapper.publishJX(companyId, staVersionNew.getId());
        } else {
            throw new ServiceException("类型错误");
        }
        return 200;
    }
}
